﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Cinser.DataTranslate.TaskManager
{
    public static class TaskManager
    {
        public static void RunTaskWithWinProgressPlus(TaskBase task,TaskOperation operationName = TaskOperation.Begin)
        {
            Cinser.Common.Wpf.Controls.WinProgressPlus progress = null;
            if (operationName == TaskOperation.Repair)
            {
                progress = new Cinser.Common.Wpf.Controls.WinProgressPlus(task.TaskName, new Cinser.Common.Wpf.Controls.NoReturnHandler(task.Repair));
            }
            else if (operationName == TaskOperation.Begin)
            {
                progress = new Cinser.Common.Wpf.Controls.WinProgressPlus(task.TaskName, new Cinser.Common.Wpf.Controls.NoReturnHandler(task.Execute));
            }
            else if (operationName == TaskOperation.DoRepeat)
            {
                //让task自动生成一个新logId
                task.LogId = "";
                progress = new Cinser.Common.Wpf.Controls.WinProgressPlus(task.TaskName, new Cinser.Common.Wpf.Controls.NoReturnHandler(task.Execute));
            }

            task.MessageAdded = (msg) =>
            {
                Cinser.Common.Wpf.Controls.ProgImageTypeEm msgType = Cinser.Common.Wpf.Controls.ProgImageTypeEm.None;
                if (msg.Type == Cinser.Common.Model.Message.MessageType.Error || msg.Type == Cinser.Common.Model.Message.MessageType.Exception)
                    msgType = Cinser.Common.Wpf.Controls.ProgImageTypeEm.Error;
                else if (msg.Type == Cinser.Common.Model.Message.MessageType.Info || msg.Type == Cinser.Common.Model.Message.MessageType.Successed)
                    msgType = Cinser.Common.Wpf.Controls.ProgImageTypeEm.Succeed;
                else if (msg.Type == Cinser.Common.Model.Message.MessageType.Warning)
                    msgType = Cinser.Common.Wpf.Controls.ProgImageTypeEm.Warn;
                progress.ContentList.Append(new Cinser.Common.Wpf.Controls.ProgressListItem(DateTime.Now.ToString() + "：" + msg.MsgContent, msgType));
            };
            task.PropertyChanged += (s1, e1) =>
            {
                switch (e1.PropertyName)
                {
                    case "Introduce":
                        {
                            progress.CurrentOperation = task.Introduce;
                        } break;
                    case "Subtitle":
                        {
                            progress.Subtitle = task.Subtitle;

                        } break;
                    case "SubTaskFinishPercent":
                        {
                            progress.CurrentPercent = task.SubTaskFinishPercent;
                            progress.Subtitle = task.Subtitle + string.Format("({0}/{1})", task.SubTaskCount, task.FinishedSubTaskCount);
                        } break;
                    default:
                        break;
                }
            };
            progress.Show();
        }


    }
}
